WEBVTT

00:00.970 --> 00:06.580
In this video we will learn some more application soft rolling statistics and finance in the last video

00:06.580 --> 00:12.220
we have learned how to calculate simple moving averages for stock prices and in this video we will have

00:12.220 --> 00:18.840
a look at the rolling performance of stocks and financial securities and we've already learned that

00:18.840 --> 00:24.500
the risk and return are the most important performance metrics for financial securities like stocks

00:25.040 --> 00:28.170
and in the finance and investment funds industry.

00:28.370 --> 00:34.340
It's quite standard to report risk and return of financial securities for the last three years based

00:34.340 --> 00:35.630
on monthly data.

00:35.990 --> 00:43.180
So that means that the return is reported as the annualized average monthly return over the last the

00:43.220 --> 00:50.990
thirty six month period and the risk is typically reported as the annualized standard deviation of monthly

00:50.990 --> 00:52.580
returns over that period.

00:53.480 --> 00:59.930
So in the next minutes he will calculate rolling risk and return for the S&amp;P 500 and we will find out

01:00.230 --> 01:04.280
if and to what extent risk and to try and change over time.

01:04.280 --> 01:04.700
All right.

01:04.700 --> 01:05.380
So let's start.

01:05.390 --> 01:10.930
And first of all we import pandas an umpire and also met plot lip as always.

01:11.240 --> 01:19.130
Then we are working with the S&amp;P 500 see a sweet fire and of course we want to pass um the date column

01:19.430 --> 01:23.230
to a date time index and we only want to use them.

01:23.230 --> 01:30.020
The column stayed and the closing prices and uh actually we save for the data frame and the variable

01:30.050 --> 01:31.800
S&amp;P 500.

01:32.510 --> 01:33.800
So let's have a look here.

01:34.130 --> 01:40.960
So these are the first five rows starting at the very last day of the year nineteen hundred and seventy.

01:41.630 --> 01:48.740
And as I said before it's quite standard in uh the finance and investment industry to report monthly

01:48.740 --> 01:49.890
returns.

01:49.910 --> 01:57.050
So as we have here daily data or daily timestamps with daily price information first of all we need

01:57.050 --> 02:04.240
to read sample our data frame to monthly and we can do the same with other release sample method and

02:04.250 --> 02:10.270
we too see a monthly and actually you want to have the very last trading price of each month.

02:10.370 --> 02:13.800
And finally we also past the period to the kind parameter.

02:13.800 --> 02:17.020
So let's run through the sell and let's have a look.

02:17.030 --> 02:19.700
So here we have monthly price data.

02:20.120 --> 02:25.720
And on the left hand side we have now a period index as we pass superior to the kind parameter.

02:25.790 --> 02:28.400
So we have a December nineteen hundred seventy.

02:28.400 --> 02:33.590
January nineteen hundred seventy one and two we have our column close.

02:33.590 --> 02:37.330
And here we have for the very last trading price of each month.

02:37.340 --> 02:41.910
So now we have monthly data and we want to calculate monthly returns.

02:41.930 --> 02:46.280
And we can do this here with the percentage change method.

02:46.490 --> 02:49.260
So we change here the read sample method.

02:49.490 --> 02:52.430
The last method and the percentage change method.

02:52.880 --> 02:57.260
And finally we want to drop them all rows with an A values.

02:57.260 --> 03:04.640
So if you calculate the percentage change then in the very first row we will have a uh and a value.

03:04.640 --> 03:07.910
So therefore we you see also the drop in a method.

03:08.060 --> 03:11.310
So we are calculating your monthly returns and we save.

03:11.360 --> 03:14.840
The resulting data frame and the variable monthly returns.

03:14.840 --> 03:18.470
So let's do this here and let's have a look

03:21.270 --> 03:23.610
so here we have the monthly returns.

03:23.610 --> 03:29.040
So for example the return in January in nineteen hundred seventy one was 4 percent.

03:29.040 --> 03:35.430
So this is exactly the change from ninety two point one five to ninety five point eighty seven.

03:35.430 --> 03:38.220
So this is a monthly return of 4 percent.

03:38.400 --> 03:42.750
And actually here now a new data for a monthly return we have actually the monthly returns starting

03:42.750 --> 03:49.760
from January nineteen hundred seventy one and to December 2000 and 18.

03:49.770 --> 03:52.170
Now we want to calculate rolling statistics.

03:52.590 --> 04:00.660
So we want to calculate the mean are the average return of the immediately preceding 36 months.

04:00.870 --> 04:06.960
And we want to do this for timestamps here and therefore we use here the rolling method and the window

04:06.960 --> 04:08.810
is a thirty six.

04:08.820 --> 04:16.410
So on each timestamp we actually group uh the last the 36 month and then we change the mean method to

04:16.410 --> 04:19.410
calculate the mean return.

04:19.500 --> 04:21.810
The mean rolling return.

04:21.810 --> 04:26.400
And by doing so we would end up with average monthly returns.

04:26.400 --> 04:31.570
And it's also quite standard in the finance industry to annualized returns.

04:31.680 --> 04:39.030
So to have kind of a feeling how the return would look like over a complete year and therefore we multiply

04:39.030 --> 04:42.480
the monthly rolling returns by twelve.

04:42.510 --> 04:44.540
So let's do this here.

04:44.580 --> 04:52.530
Consequently we get here now the annualized average monthly returns over the last uh thirty six periods

04:53.010 --> 04:55.890
and in kind of a rolling statistics manner.

04:55.890 --> 05:02.160
So we have for each timestamp actually the annualized mean return for the last three years.

05:02.550 --> 05:07.760
And finally we create in the new column return so let's do this here.

05:07.770 --> 05:14.770
So now we have added the rolling return of the S&amp;P 500 index and now we want to add also the year risk

05:14.880 --> 05:22.560
and um the risk is typically calculated as a standard deviation of monthly returns and we can actually

05:22.560 --> 05:27.840
simply calculate the rolling standard deviation of monthly returns by a take here.

05:27.840 --> 05:34.620
The monthly returns based on the close price then we change the rolling method and of course also here

05:34.620 --> 05:41.850
we pass um thirty six to the window parameter and then we calculate a standard deviation.

05:41.850 --> 05:46.830
So this would uh then reside in the standard deviation of monthly returns.

05:47.190 --> 05:50.170
So to say the monthly standard deviation.

05:50.340 --> 05:56.970
And finally we can also annualize a monthly standard to creation by multiplying with the square root

05:57.030 --> 05:58.040
of twelve.

05:58.050 --> 05:59.140
So let's do this here.

06:00.240 --> 06:11.030
So here we have the rolling standard deviation and finally we can also create a new column risk.

06:11.030 --> 06:17.270
So now we have two new columns the return and risk for our monthly return data frame and then we could

06:17.270 --> 06:18.620
also drop and a rose.

06:18.620 --> 06:24.920
So the first the thirty five rows should contain and a values in both new columns.

06:24.920 --> 06:30.820
So let's do this here and now let's finally inspect our monthly return data frame.

06:30.860 --> 06:35.580
So here we have the monthly returns of the closing prices.

06:35.720 --> 06:38.450
Then we have uh the rolling return.

06:38.750 --> 06:43.540
So this is uh the return of the last uh 36 months.

06:43.550 --> 06:46.160
And we have here the rolling risk.

06:46.160 --> 06:53.310
And apparently he had the first rolling such as Six we have on December nineteen hundred seventy three.

06:53.720 --> 07:00.620
And we could say that um the annualized average monthly return for the preceding three years is actually

07:00.620 --> 07:02.120
two point six percent.

07:02.600 --> 07:05.130
And the risk is twelve percent.

07:05.150 --> 07:12.020
So let's also have a look at the last five rows here and we could say that the annualized average monthly

07:12.020 --> 07:20.990
return from January to 16 to December to eighteen is seven point one percent and the annualized standard

07:20.990 --> 07:25.670
deviation of monthly returns in this period is 11 percent.

07:25.730 --> 07:32.660
Now we can also plot the rolling statistics so the column return and risk and we can do this year of

07:32.660 --> 07:40.280
course with the plot method and we take the last two columns of our data for a monthly returns.

07:40.280 --> 07:41.150
So let's have a look

07:44.750 --> 07:51.980
and here you can actually see that change of Risk and Return of the S&amp;P 500 index over time.

07:51.980 --> 08:00.260
So we have uh the blue line or the blue curve which is the rolling return for the last three years.

08:00.440 --> 08:06.780
And the green line is actually the risk or the standard deviation of monthly returns for the preceding

08:06.800 --> 08:08.970
uh three years period.

08:09.230 --> 08:14.590
And we can also see here that depending on the market environment and depending on the time.

08:14.780 --> 08:18.460
So risk and the return are quite volatile here.

08:18.470 --> 08:26.560
So for example here in 2009 2010 we can see here a quite high risk and a very low return.

08:26.570 --> 08:30.500
So minus 10 percent annualized and the opposite.

08:30.500 --> 08:36.080
We can see for example here in the middle or the end of the 90s.

08:36.140 --> 08:37.950
So we have quite high returns.

08:37.940 --> 08:39.830
So what 20 percent.

08:39.830 --> 08:42.760
And we have quite a low risk here.

08:42.830 --> 08:51.170
So it seems like investing in the S&amp;P 500 over a period of three years is quite volatile and uh really

08:51.170 --> 08:57.190
depends on the market environment and the time you are investing so it can be a good investment.

08:57.200 --> 09:00.980
But it can also be a very poor investment.

09:00.980 --> 09:07.340
And from the two lines see we can also see that the risk and return could be negatively correlated.

09:07.340 --> 09:09.350
So whenever the risk is quite high.

09:09.350 --> 09:15.570
So for example here or here then the returns are pretty low and vice versa actually.

09:15.620 --> 09:20.020
So here we have a higher return and a fairly low risk.

09:20.240 --> 09:24.290
And also here we have a high return and the risk is fairly low.

09:24.860 --> 09:30.560
So it might be worth it to also calculate the correlation between the rolling return and the rolling

09:30.560 --> 09:31.550
risk.

09:31.550 --> 09:34.290
And we can do this with the car method.

09:34.400 --> 09:38.760
So we take the two columns here of monthly returns.

09:38.760 --> 09:41.970
So the last two columns and the change the car method.

09:41.990 --> 09:50.060
So let's do this and we can see here that there is a significant negative correlation between risk and

09:50.060 --> 09:50.530
return.

09:50.540 --> 09:52.110
So minus 0 point.

09:52.190 --> 09:53.270
3 2.

09:53.900 --> 10:01.640
And we can also create a scatter plot with all parcels of risk and return for the last 40 years.

10:01.700 --> 10:05.030
So we plotted the columns the risk and return.

10:05.030 --> 10:12.080
And we use here a scatter plot and on the x axis we want to have the risk a standard deviation and on

10:12.080 --> 10:14.810
the y axis the return.

10:14.900 --> 10:16.040
So let's do this here

10:19.240 --> 10:24.580
so here we have the risk on the x axis and the return on the y axis.

10:24.580 --> 10:30.490
And actually each point is a combination of rolling risk and rolling return for a particular month from

10:30.490 --> 10:34.420
nineteen hundred seventy three until 2018.

10:34.420 --> 10:39.970
So for example we have quite good three year periods with a quiet low risk.

10:39.970 --> 10:44.710
So twelve percent and a high return over 20 percent.

10:44.710 --> 10:49.980
And we can also see periods with a quite poor performance of the S&amp;P 500.

10:50.380 --> 10:54.140
So for example a fairly high standard deviation or risk.

10:54.190 --> 11:01.620
So what 20 percent and a mean return of less than minus 10 percent.

11:01.630 --> 11:10.090
So again we found out that investing in the S&amp;P 500 over a period of three years is a quite risky and

11:10.420 --> 11:12.060
volatile investment.

11:12.190 --> 11:18.820
And in the next two videos we want to investigate how this picture changes if we increase our investment

11:18.820 --> 11:22.120
period also decrease our investment period.

11:22.180 --> 11:24.850
So I hope to see you also in the next videos by.
